 package com.hddz.acquisition;

 import android.app.Activity;
 import android.app.AlertDialog;
 import android.content.DialogInterface;
 import android.os.Handler;
 import android.os.Message;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.EditText;
 import android.widget.ImageView;
 import android.widget.TextView;
 import android.widget.Toast;


 public class ErrorInfoUtil {
     private Activity mContext;
     /**
      * 级别0  操作成功，alert
      */
     public static final int LEVEL_INFO=0;
     /**
      * 级别1   show toast
      */
     public static final int LEVEL_TOAST=1;
     /**
      * 级别2    显示普通alert对话
      */
     public static final int LEVEL_WARN=2;
     /**
      * 级别3  显示带桔红色警示框
      */
     public static final int LEVEL_ERROR=3;
     /**
      * 级别4 显示红色X框
      */
     public static final int LEVEL_FATAL=4;
     /**
      * 手机号已注册提醒
      */
     public static final int REG_PHONE=5;
     private Toast toast = null;
     private AlertDialog.Builder adb;
     private SoundUtil mSoundUtil;
     private LayoutInflater inflater;

     public ErrorInfoUtil(Activity text, SoundUtil mSoundUtil){
         mContext = text;
         this.mSoundUtil = mSoundUtil;
         inflater = mContext.getLayoutInflater();
     }


     /**
      * 默认显示显普通对话框
      * @param message
      */
     public void showError(Object message){
         showError(message,LEVEL_WARN);
     }

     public void showError(Object message, int level){
         switch (level) {
         case LEVEL_TOAST:
             showToast(message);
             break;
         default:
             if(message!=null){
                 String className = message.getClass().getName();
                 if(className.indexOf("ception")>=0){
                     Exception ex = (Exception) message;
                     ex.printStackTrace();
                 }
             }
             showLevel234(message,level,null,null);
             break;
         }
     }
     public void showError(Object message, int level, EditText edit){
         switch (level) {
         case LEVEL_TOAST:
             showToast(message);
             break;

         default:
             showLevel234(message,level,edit,null);
             break;
         }
     }


     public void showError(Object message, int level, Handler hander){
         switch (level) {
         case LEVEL_TOAST:
             showToast(message);
             break;

         default:
             showLevel234(message,level,null,hander);
             break;
         }
     }


     private void showLevel234(Object message, int level, final EditText mEditText, final Handler mHandler){
         try {
             adb = new AlertDialog.Builder(mContext);
             View layout = inflater.inflate(R.layout.customer_dialog, null);
             TextView msgTxt = (TextView) layout.findViewById(R.id.dialog_mesage_txt);
             ImageView msgImg = (ImageView) layout.findViewById(R.id.dialog_icon_img);
             String className = message.getClass().getName();
             if(message==null){
                 msgTxt.setText("系统错误");
             }else{
                 if(className.indexOf("ception")>=0){
                     Exception ex = (Exception) message;
                     msgTxt.setText(ex.getMessage());
                 }else{
                     msgTxt.setText(message.toString());
                 }
             }

             if(level == LEVEL_INFO){
                 mSoundUtil.playEnd();
                 adb.setTitle("操作提示");
                 msgImg.setImageDrawable(mContext.getResources().getDrawable(R.drawable.dilog_icon_succes));
             }else if(level==LEVEL_WARN){
                 mSoundUtil.playTip();
                 adb.setTitle("温馨提示");
                 msgImg.setImageDrawable(mContext.getResources().getDrawable(R.drawable.dilog_icon_info));
             }else{
                 mSoundUtil.playAir();
                 if(level==LEVEL_ERROR){
                     adb.setTitle("错误");

                     msgImg.setImageDrawable(mContext.getResources().getDrawable(R.drawable.dilog_icon_careful));
                 }else{
                     adb.setTitle("严重错误");
                     msgImg.setImageDrawable(mContext.getResources().getDrawable(R.drawable.dilog_icon_danger));
                 }
             }
             adb.setView(layout);
             adb.setNegativeButton("确认", new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int which) {
                     dialog.cancel();
                     adb = null;
                     if(mEditText!=null){
                         mEditText.requestFocus();
                         return ;
                     }
                     if(mHandler!=null){
                         mHandler.sendMessage(new Message());
                     }
                 }
             });
             adb.setCancelable(false);
             adb.show();
         } catch (Exception e) {
             e.printStackTrace();
         }
     }


     public void showToast(Object message){
         if (toast != null) {
             toast.cancel();
         }
         mSoundUtil.playTip();
         toast = Toast.makeText(mContext, message.toString(), Toast.LENGTH_SHORT);
         toast.show();
     }

 }
